import axios from 'axios'
import Message from 'iview/src/components/message';
import Cookie from 'js-cookie'

window.apiHost = 'http://114.215.110.36:8989/api/v1'

const post = (url, data, option = {loading: false, errmsg: '请求发生异常'}) => {
    let token = Cookie.get('csrftoken')
    return axios({
        url: apiHost + url,
        data: data,
        withCredentials: true,
        headers: {
            'X-CSRFToken': window.__ToKeN__,
        },
        method: option.method || 'POST'
    }).catch(e => {
        if (e.response) {
            return e.response
        } else {
            console.warn(e);
            Message.error('请求发生异常')
        }
    }).then(res => {
        if (/^2\d\d/.test(res.status)) {
            return {
                success: true,
                data: res.data,
                message: ''
            }
        } else {
            let message
            let keys = Object.keys(res.data || {})
            keys.forEach(e => {
                if (res.data[e]) {
                    message = res.data[e]
                }
            })
            Message.warning(message || option.errmsg);
            return {
                success: false,
                message: message
            }
        }
    })
};

const get = (url, params, option = {loading: false, errmsg: '请求发生异常'}) => {

    return axios({
        url: apiHost + url,
        withCredentials: true,
        params: params ? Object.assign(params, {time: new Date().getTime()}) : {time: new Date().getTime()},
        method: 'GET'
    }).catch(e => {
        if (e.response) {
            return e.response
        } else {
            console.warn(e);
            Message.error('请求发生异常')
        }
    }).then(res => {
        if (/^2\d\d/.test(res.status)) {
            return {
                success: true,
                data: res.data,
                message: ''
            }
        } else {
            let message
            let keys = Object.keys(res.data.keys || {})
            keys.forEach(e => {
                if (res.data[e]) {
                    message = res.data[e]
                }
            })
            Message.warning(message || option.errmsg);
            return {
                success: false,
                message: message
            }
        }
    })
};

const APIS = {
    // 系统
    login (data) {
        const url = '/login/';
        return post(url, data, {errmsg: '登录失败'})
    },
    logout () {
        const url = '/logout/';
        return post(url, null, {errmsg: '登出失败'})
    },
    getUserInfo () {
        const url = '/user_info/';
        return get(url, null, {errmsg: '获取用户信息失败'})
    },
    // 活动相关
    getCameraID (code) {
        const url = '/search_camera/';
        return get(url, {code}, {errmsg: '获取机位ID失败'})
    },
    getBanners (actId) {
        const url = `/activities/${actId}/banners/`;
        return get(url, null, {errmsg: '获取banner失败'})
    },
    delBanner (actId, bannerId) {
        const url = `/activities/${actId}/banners/${bannerId}/`;
        return post(url, null, {errmsg: '删除banner失败', method: 'DELETE'})
    },
    getActivityList (params) {
        const url = '/activities/';
        return get(url, params, {errmsg: '获取活动列表失败'})
    },
    getPartnerList (params) {
        const url = `/partners/`
        return get(url, params, {errmsg: '获取合作方列表失败'})
    },
    getPhotographerList (params) {
        const url = '/photographers/';
        return get(url, params, {errmsg: '获取摄影师列表失败'})
    },
    joinActivity (activity_pk, camera_pk) {
        const url = `/activities/${activity_pk}/cameras/${camera_pk}/retouchers/`;
        return post(url, null, {errmsg: '加入活动失败'})
    },
    createActivity (data) {
        const url = '/activities/';
        return post(url, data, {errmsg: '创建活动失败'})
    },
    endActivity (id) {
        const url = `/activities/${id}/`;
        return post(url, {status: 3}, {errmsg: '结束活动失败', method: 'PATCH'})
    },
    // 照片列表相关
    getRetoucherName (id) {
        const url = `/retouchers/${id}/`
        return get(url)
    },
    getBasePhotoList (params, actID, cameraID) {
        const url = `/activities/${actID}/cameras/${cameraID}/base_pictures/`
        return get(url, params)
    },
    getJXPhotoList (page, actID) {
        const url = `/activities/${actID}/truing_pictures/`
        return get(url, {page})
    },
    getCameraList (actID) {
        const url = `/activities/${actID}/cameras/`
        return get(url, null, {errmsg: '获取机位列表失败'})
    },
    addPhoto (activity_pk, camera_pk, id) {
        let postData = new FormData()
        postData.append('id', id)
        const url = `/choose_pic/`
        return post(url, postData, {errmsg: '选择照片失败'})
    },
    thmDownload (activity_pk, camera_pk, id) {
        const url = `/activities/${activity_pk}/cameras/${camera_pk}/base_pictures/${id}/`
        return post(url, {downloaded_compressed: true}, {method: 'PATCH'})
    },
    oriDownload (activity_pk, camera_pk, id) {
        const url = `/activities/${activity_pk}/cameras/${camera_pk}/base_pictures/${id}/`
        return post(url, {downloaded_original: true}, {method: 'PATCH'})
    },
    delPhoto () {
        const url = '/delPhoto';
        return post(url, data)
    },
    // 用户管理相关
    getUserList (type, params) {
        const url = `/${type}/`
        return get(url, params, {errmsg: '获取用户列表失败'})
    },
    createOrEditUser (type, data, isModify) {
        const url = isModify ? `/${type}/${data.id}/` : `/${type}/`
        return post(url, data, {method: isModify ? 'PUT' : 'POST'})
    }

}

export default APIS